/**
 * Created by wangxiaoqiang on 2017/8/23.
 */
const pageSize = 10;//设置的分页每一页请求的数量
// const rootPath = "https://cake.sanploy.cn";//请求数据的根路径
// const fileServerPath = "https://cake.sanploy.cn";//图片服务器请求根路径


const rootPath = "http://localhost";//请求数据的根路径
const fileServerPath = "http://localhost";//图片服务器请求根路径

/**
 * 退出系统方法
 */
let logout = ()=>{
    $.get(rootPath+"/logout",(res)=>{
        location.href = rootPath+"/back/view/login.html"
    })
};
//点击退出系统
$("#logout").click(()=>{
    logout()
});

if(localStorage.getItem("username")){
    if(localStorage.getItem("authoritys") === "ROLE_SUPER"){
        $(".admin_type").text("超级管理员")
    }else {
        $(".admin_type").text("管理员")
    }
    $(".admin_name").text(localStorage.getItem("username"))
}

//设置点击无效并显示加载
let setStatus = {
    setDisable:(jqDom)=>{
        jqDom.attr("disable","disable");
        jqDom.empty();
        jqDom.append(`<i class="fa fa-spinner fa-pulse"></i>`);
    },
    setEnable:(jqDom,text)=>{
        jqDom.attr("disable","false");
        jqDom.empty();
        jqDom.append(text);
    }
};

/**
 * 图片上传公共方法
 * @param fromData
 * @returns {Promise}
 */
let uploadFile = function (files) {
    //数据添加到fromData
    let fromData = new FormData();
    fromData.append("projectName", "aamm");
    fromData.append("directoryName", "photo");
    for(let i = 0;i <files.length;i++){
        fromData.append(files[i].name,files[i])
    }
    return new Promise(function (exe, re) {
        $.ajax({
            url:fileServerPath+"/upload/normal",
            type:"POST",
            data: fromData,
            contentType: false,
            processData: false,
            success: function (res) {
                exe(res)
            },
            error:function (res) {
                re(res)
            }
        })
    });
};

/**
 * 回车事件
 * @param inpDom
 * @param fn
 */
let gotoPage = (inpDom,fn)=>{
    inpDom.onkeydown=function(event){
        let e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode===13){ // enter 键
            let inpVal = inpDom.value;
            fn(inpVal)
        }
    };
};

let setdot = function () {
    $.get(rootPath+"/message/count",{is_read:false}, (res)=> {
        if(res.meta.code === 200){
            if(res.data >0){
                $(".message_count").addClass("message_dot")
            }else {
                $(".message_count").removeClass("message_dot")
            }
        }else {
            alert("请求失败--"+res.meta.message)
        }
    })
};
setdot();


setInterval(function () {
    $.get(rootPath+"/message/newMessage",function (res) {
        if(res.meta.code === 200 && res.data){
            if(document.getElementById("video")){
                document.getElementById("video").play()
            }else {
                $("body").append(`<video src="${rootPath}/back/video/ee.mp3" id = "video" style="display: none" controls="controls" autoplay></video>`);
            }
            let message_warp_info = $(".message_warp_info");
            message_warp_info.empty();
            message_warp_info.append(res.data);
            message_warp_info.css({marginLeft:-(message_warp_info.width()/2)});
            message_warp_info.show();
            setTimeout(function () {
                message_warp_info.hide();
            },10000)
        }
    });
},2000);
